package com.gw.smscheck.controller.sys;

import com.gw.smscheck.entity.sys.*;
import com.gw.smscheck.service.sys.LoginUserService;
import com.gw.smscheck.service.sys.MenuService;
import com.gw.smscheck.util.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 舆情检索接口
 *
 * @author ASUS
 */
@Controller
@RequestMapping("/menuController")
@Api(description = "【菜单管理接口】")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private LoginUserService loginUserService;
    /**
     * 跳转到菜单页面
     *
     * @return
     */
    @RequestMapping("/goMenu")
    @ApiIgnore
    public String goMenu() {
        return "sys/menu";
    }
    /**
     * 动态菜单列表查询
     * @return
     */
    @ApiOperation(value="动态菜单列表查询", notes="动态菜单列表查询")
    @RequestMapping(value = "/dynamicPageSelect", method = { RequestMethod.POST })
    @ResponseBody
    public String dynamicPageSelect(Menu dto,HttpServletRequest req) throws Exception {
        HttpSession session = req.getSession();
        Integer userId =Integer.valueOf((Integer) session.getAttribute("userId"));
        System.out.println("========打印用户ID值=========="+userId);
        LoginUser loginUser = new LoginUser();
        loginUser.setId(userId);
        LoginUser loginUserTemp;
        loginUserTemp= loginUserService.findById(loginUser);
        System.out.println("========打印临时用户信息=========="+loginUserTemp);
        Menu menu = new Menu();
        menu.setRoleId(loginUserTemp.getRoleId());
        return ResultObj.successResultObj( menuService.searchAllSysMenus(menu));
    }
    /**
     * 列表查询
     * @return
     */
    @ApiOperation(value="列表查询", notes="列表查询")
    @RequestMapping(value = "/searchList", method = { RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String pageSelect(Menu dto) {
       return ResultObj.successResultObj(menuService.searchList(dto));

    }
    /**
     * 添加
     * @return
     */
    @ApiOperation(value="添加", notes="添加")
    @RequestMapping(value = "/add", method = { RequestMethod.POST })
    @ResponseBody
    public String add(Menu dto, HttpServletRequest req) {
        HttpSession session = req.getSession();
        String userName = String.valueOf(session.getAttribute("userName"));
        dto.setCreateUser(userName);
        System.out.println(userName);
        menuService.add(dto);
        return ResultObj.successResultObj(dto);

    }
    /**
     * 修改
     * @param menu
     * @return
     */
    @ApiOperation(value="修改", notes="修改")
    @RequestMapping(value="/update",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String update(Menu menu){
        return ResultObj.successResultObj(menuService.update(menu));
    }
    /**
     * 删除
     * @param menu
     * @return
     */
    @ApiOperation(value="删除", notes="删除")
    @RequestMapping(value="/del",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String del(Menu menu){
        menuService.del(menu);
        return ResultObj.successResultObj("删除成功！");
    }

    /**
     * 授权菜单查询
     * @Title: queryAllSysMenus
     * @return List<TreeModel>
     * @throws Exception 抛异常
     */
    @ApiOperation(value="授权菜单查询", notes="授权菜单查询")
    @RequestMapping(value="/queryAllSysMenus",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<TreeModel> queryAllSysMenus(Role r) throws Exception
    {
        System.out.println("进来了====================");
        Menu m = new Menu();
        m.setRoleId(r.getId());
        return this.menuService.queryAllSysMenus(m);
    }
    /**
     * 角色授权保存
     * @Title: authorPower
     * @param rId 用户id
     * @param rightIds 菜单id
     * @return ParameterObject
     * @throws Exception 抛异常
     */
    @ApiOperation(value="角色授权保存", notes="角色授权保存")
    @RequestMapping(value="/addUserPower",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public  ParameterObject addUserPower(Integer rId, Integer[] rightIds)
    {
        ParameterObject result = new ParameterObject();
        try
        {
            if (menuService.saveRoleAndPower(rId, rightIds))
            {
                result.flag(true);
                result.code(10001);
                result.msg("授权成功！");
            }
            return result;
        }
        catch (Exception e)
        {
        }
        result.flag(false);
        result.msg("授权失败！");
        return result;
    }

}
